<?php
class ProductsM extends CI_Model {
	
	private $s_category = FALSE;
	private $s_title = FALSE;
	private $s_promotion = FALSE;
	private static $search_terms = array ('s_category', 's_title', 's_promotion' );
	var $table = 'products';
	var $gallery_path;
	var $gallery_path_url;
	
	function __construct() {
		// call the Model constructor
		parent::__construct ();
		$this->search_terms = self::$search_terms;
		$this->_check_search_session ();
		$this->gallery_path = realpath ( APPPATH . '../pictures' );
		$this->gallery_path_url = base_url () . 'pictures/';
	    log_message('debug',$this->gallery_path);
	}
	
	function _check_search_session() {
		
		foreach ( $this->search_terms as $key => $val ) {
			
			$chk = $this->session->userdata ( $val );
			if ($chk !== FALSE) {
//				printf('--'.$val.'== ');
				if (empty ( $chk )) {
//				    printf('val is ampy');
					$this->$val = FALSE;
				} else {
//				    printf('val is='.$chk);
					$this->$val = $chk;
				}
				unset ( $chk );
			
			}else {
			  $this->$val = FALSE;
			}
		}
	}
	
	function _filter() {
		
		if ($this->s_category !== FALSE) {
		
			$this->db->where ( "category = " . $this->s_category . " " );
		}
		
		if ($this->s_title !== FALSE) {
		
			$this->db->where ( "title LIKE('%" . $this->s_title . "%') " );
		
		}
		
		if ($this->s_promotion !== FALSE) {
		
			$this->db->where ( "is_promotion", $this->s_promotion );
		
		}
	
	}
	
	function get_all($limit, $offset) {
		$this->_check_search_session();
		//		$this->db->select ( "*");
		$this->db->select ( "products.id, products.title, products.description, categories.title as category, products.company,products.link, products.is_promotion,
							(SELECT p_pictures.name
                               FROM ci_auth_test.p_pictures
                              WHERE p_pictures.id_product =products.id limit 1
                            ) as pic" );
		$this->db->join ( 'categories', 'products.category=categories.id', 'left' );
		$this->_filter ();
		
		$this->db->order_by ( "id", "DESC" );
		$this->db->limit ( $limit, $offset );
		$query = $this->db->get ( $this->table );
		
		if ($query->num_rows () > 0) {
			
			//			return $query->result();
			return $query->result_array ();
		
		}
		return FALSE;
	
	}
	
	function count_all() {
		
		$this->db->select ( "id" );
		$this->_check_search_session();
		$this->_filter ();
		$query = $this->db->get ( $this->table );
		
		return $query->num_rows ();
	
	}
	function count_all_promotionsd() {
		
		$this->db->select ( "id" );
		$this->_check_search_session();
		$this->_filter ();
		$this->db->where ( "is_promotions=1" );
		$query = $this->db->get ( $this->table );
		
		return $query->num_rows ();
	
	}
	function getAllProducts() {
	    
		$this->load->database ();
		$query = $this->db->get ( $this->table );
		if ($query->num_rows () > 0) {
			// return result set as an associative array
			return $query->result_array ();
		}
	}
	
	function getProductsWhere($field, $param) {
		$this->db->where ( $field, $param );
		$query = $this->db->get ( $this->table );
		// return result set as an associative array
		return $query->result_array ();
	}
function getProductsWithCategoryWhere($field, $param) {
        $this->db->select($this->table.'.*,categories.title as cat_title');
		$this->db->from($this->table);
		$this->db->join('categories',$this->table.'.category = categories.id');
		$this->db->where ( $field, $param );
		$query = $this->db->get ();
		// return result set as an associative array
		return $query->result_array ();
	}
	function getProductsWhere2($field, $param) {
	    $this->db->select('title as color,name');
		$this->db->where ( $field, $param );
		$query = $this->db->get ( $this->table );
		// return result set as an associative array
		return $query->result_array ();
	}
//	function getUsersWhere($field, $param) {
//		$this->db->where ( $field, $param );
//		$query = $this->db->get ( $this->table );
//		// return result set as an associative array
//		return $query->result_array ();
//	}
	
	/**
	 * По ид на продуцт, жрасхта категорията 
	 * @param unknown_type $id
	 */
	function getProductCategory($id){
	    
	}
	// get total number of users
	function getNumUsers() {
		
		//		$this->load->database();
		return $this->db->count_all ( $this->table );
	
		//		 return 10;
	}
	
	function get_images($prodictId = null) {
		//	print_r($this->gallery_path.'/'.$prodictId );
		if (! isset ( $prodictId ) || is_null ( $prodictId ))
			return array ();
		
			
		$this->db->select('title as color,name,id as pic_id');
		$this->db->where('id_product',$prodictId);
		$query = $this->db->get ( 'p_pictures' );
		$rezPics=$query->result_array ();
		
		$images = array ();
		foreach ($rezPics as $pic){
    		//Ako ja njama direktorijata za tova ID, ja sazdavame za da ne garmi grosno	
    		if (! file_exists ( $this->gallery_path . '/' . $prodictId )) {
    			
    			mkdir ( $this->gallery_path . '/' . $prodictId );
    			mkdir ( $this->gallery_path . '/' . $prodictId . '/' . '/' . 'thumbs' );
    		}
    		$images1=  array ('url' => $this->gallery_path_url . $prodictId . '/' . $pic['name'],
			                    'thumb_url' => $this->gallery_path_url . '/' . $prodictId . '/' . 'thumbs/' . $pic['name'],
    		                    'color'=>$pic['color'] ,
    		                    'name'=>$pic['name'],
    		                    'pic_id'=>$pic['pic_id']);
    		$images[$pic['pic_id']]=$images1;
    		
		}
		
		
		return $images;
	}
	function get_images_old($prodictId = null) {
		//	print_r($this->gallery_path.'/'.$prodictId );
		if (! isset ( $prodictId ) || is_null ( $prodictId ))
			return array ();
		
		//Ako ja njama direktorijata za tova ID, ja sazdavame za da ne garmi grosno	
		if (! file_exists ( $this->gallery_path . '/' . $prodictId )) {
			
			mkdir ( $this->gallery_path . '/' . $prodictId );
			mkdir ( $this->gallery_path . '/' . $prodictId . '/' . '/' . 'thumbs' );
		}
		
		$files = scandir ( $this->gallery_path . '/' . $prodictId );
		$files = array_diff ( $files, array ('.', '..', 'thumbs','.svn' ) );
		
		$images = array ();
		
		foreach ( $files as $file ) {
			$images [] = array ('url' => $this->gallery_path_url . $prodictId . '/' . $file,
			                    'thumb_url' => $this->gallery_path_url . '/' . $prodictId . '/' . 'thumbs/' . $file );
		}
		
		return $images;
	}
	
	function delete($id=null){
	    if ($id!=null){
	        $this->db->delete($this->table, array('id' => $id));
	    }
	    return;
	}
	
	
}
?>